

(function(){
	let carTable = document.querySelector(".cartable");

	//渲染
	json.post(URL + '/api_cart', {
		status : 'viewcart',
		userId : TOKEN,
		},even =>{
		
			if(even.code != 0){
				console.log(even);
				return;
			};
			
			//验证是否有商品
			let {data} = even; // res.data;
			if(data.length == 0){
				carTable.innerHTML = '您的车车空空如也~~~';
				return;
			};
			
			let str = `
				<tr class="top" >
						<td width="120">
							<input class="check-all" type="checkbox">
							全选</td>
						<td width="310">良品</td>
						<td width="130">数量</td>
						<td width="170">单价(元)</td>
						<td width="170">小计(元)</td>
						<td width="">操作</td>
					</tr>
			`;
			
			data.forEach(item =>{
				str +=`
				<tr class="list" >
					<td clas="flex-row-center" width="120">
						<input goods-id="${item.goods_id}" class="check" type="checkbox">
						<a><img src="${item.goods_thumb}"></a>
						</td>
					<td width="310">${item.goods_name}</td>
					<td width="130">
					<span  class="reduce" goods-id="${item.goods_id}" price="${item.price}" >-</span>
					&nbsp;<span>${item.goods_number}</span>&nbsp;
					<span class="add" goods-id="${item.goods_id}" price="${item.price}">+</span></td>
					</td>
					<td class="price" width="170">${item.price}</td>
					<td class="subtotal" width="170">${(item.price * parseInt(item.goods_number)).toFixed(2)}</th>
					<td class="del-btn" width=""  goods-id="${item.goods_id}">删除</td>
				</tr>
				
				`;
			});
			carTable.innerHTML = str;
		})

	//点击事件
	carTable.onclick = function(e){
		
		
		let nowClassName = e.target.className;
		console.log(nowClassName);
		//点击了全选
		if(nowClassName == 'check-all'){
			
			let aCheckAll = carTable.querySelectorAll('.check');
			//遍历设置
			aCheckAll.forEach(item => {
				//状态跟随全选
				item.checked = e.target.checked;
				//设置标记  自定义 属性
				item.setAttribute('data-goods', e.target.checked ? 'check' : '')
			});
			
			//调用总价的方法
			pricesum();
		};
		//选中商品
		if(nowClassName == 'check'){
			e.target.setAttribute("data-goods",e.target.checked? "check" : "");
			pricesum();
		}
		//点击+
		if(nowClassName == 'add'){
			let nowNumber = parseInt(e.target.previousElementSibling.innerHTML);
			if(nowNumber >= 10) return;
			
			nowNumber = ++nowNumber >= 10 ? 10 : nowNumber;
			
			json.post(URL + `/api_cart`,{
				status : 'addcart',
				userId : TOKEN,
				goodsId : e.target.getAttribute('goods-id'),
				goodsNumber : nowNumber,
			},even =>{
				console.log(even)
				if(even.code != 0) return;
				
				e.target.previousElementSibling.innerHTML = nowNumber;
			
				e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = e.target.getAttribute('price')*nowNumber
			
				pricesum()
			})
		}
		
		//点击-
		if(nowClassName == 'reduce'){
			let nowNumber = parseInt(e.target.nextElementSibling.innerHTML);
			if(nowNumber <= 1) return;
			
			nowNumber = --nowNumber <= 1 ? 1 : nowNumber;
			
			json.post(URL + `/api_cart`,{
				status : 'addcart',
				userId : TOKEN,
				goodsId : e.target.getAttribute('goods-id'),
				goodsNumber : nowNumber,
			},even =>{
				console.log(even)
				if(even.code != 0) return;
				
				 e.target.nextElementSibling.innerHTML = nowNumber;
			
				e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = e.target.getAttribute('price')*nowNumber
			
				pricesum()
			})
		}
		//点击删除
		if(nowClassName == 'del-btn'){
		
			json.post(URL + "/api_cart",{
				status : 'delcart',
				userId : TOKEN,
				goodsId : e.target.getAttribute('goods-id')
			},even =>{
				
				if(even.code != 0) return;
				
			    e.target.parentNode.remove();
				
				pricesum();
				
				carlist();
				
				
			})
		}
		
		
	}
	
	let jeisuan = document.querySelector(".jeisuan");
	
	//点击结算
	jeisuan.onclick = function(){
		let aCheckDom = document.querySelectorAll('[data-goods="check"]');
		// console.log(aCheckDom)
		if(aCheckDom == null){
			alert('请选择商品');
			return;
		};
		
		let goodsIdArr = [];
		aCheckDom.forEach(item =>{
			goodsIdArr.push(item.getAttribute("goods-id"));
		});
		let goodsId = goodsIdArr.join("&");
		console.log(goodsId)
		json.post(URL + '/api_settlement',{
				status : 'addsettlement',
				userId : TOKEN,
				from : 'cart',
				goodsId: goodsId,
		}, even =>{
			console.log(even)
			if(even.code != 0) return;
			
			location.href = 'pay.html';
		});
	
	}
	
	
	//计算总价
	function pricesum(){
		//拿到被选中的商品
		let productChack = document.querySelectorAll(`[data-goods = "check"]`)
		
		let priceAll = document.querySelector(".sum :nth-child(2) span")
		
		let sum = 0;
		productChack.forEach(item =>{
			
			let num = item.parentNode.nextElementSibling.nextElementSibling.children[1].innerHTML
			
			let price = item.parentNode.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML;
			
			sum += num * price;
		});
		
		priceAll.innerHTML = "￥" +sum;
		
	}
})();